<template>
  <a-modal
      v-model:visible="visible"
      :title="title"
      :width="800"
      @ok="handleOk"
      :loading="loading"
  >
    <a-form
        ref="formRef"
        :model="formData"
        v-bind="{labelCol: { span: 8 },wrapperCol: { span: 16 }}"
    >
      <a-form-item name="dictCode" label="编码" :rules="[{ required: true, message: '请输入名称!' }]">
        <a-input v-model:value="formData.dictCode" placeholder="请输入编码"/>
      </a-form-item>
      <a-form-item name="dictName" label="名称" :rules="[{ required: true, message: '请输入数据值!' }]">
        <a-input v-model:value="formData.dictName" placeholder="请输入名称"/>
      </a-form-item>
      <a-form-item name="description" label="字典描述">
        <a-textarea v-model:value="formData.description" placeholder="请输入字典描述" :rows="4"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { Ref, ref } from 'vue'
import { FormInstance, message } from 'ant-design-vue'
import { saveDict } from '@/api/dict'
import { useModalForm } from '@/modules/use-modules'

const formRef = ref<FormInstance>() as Ref<FormInstance>

const {
  formData,
  loading,
  visible,
  title,
  open,
  close
} = useModalForm({
  dictName: '',
  dictCode: '',
  description: ''
}, async (data: any) => {
  return { ...data }
})

const emits = defineEmits(['ok'])

const handleOk = async () => {
  await formRef.value.validate()
  await saveDict(formData.value)
  message.success('操作成功')
  emits('ok')
  close()
}

defineExpose({ open })
</script>
